#include <assert.h>
#include <string.h>
#include "ticker.h"
#include "sim.h"
#include "uarch/types.h"

extern Simulator Sim;
using namespace manifold::kernel;

/************************************************************
 * Module constructor, destructor, and associated functions.
 ************************************************************/
Ticker::Ticker ()
{
    warming = true;
}

Ticker::~Ticker (void)
{
}

void Ticker::tick()
{
//    Sim.global_clock++;

//    if (Sim.global_clock % 10000 == 0) 
//        fprintf(stderr, "(%lld).", Sim.global_clock);

    if (Global_Clock % 10000 == 0) 
        fprintf(stderr, "(%lld).", Global_Clock);

	if (warming && Sim.settings.warmup_time && (Sim.settings.sesc_nsim) > Sim.settings.warmup_time) 
	{
		fprintf(stderr,"Warmup ends: %lld", Sim.settings.sesc_nsim);
		Sim.stat_manager->clear_all();
		warming = false;
	}

   /** Repeater event processing. */
   Sim.process_repeater_event_queue ();
/////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////
#if !SIM_WITH_ZESTO
    bool work = false;
    for (int i = 0; i < Sim.settings.num_nodes ; i++)
    {
        if (Sim.get_PR(i)->active_contexts () > 0)
        {
            work = true;
        }
    }

    if (work == false)
    {
        static int flushing_cnt = 10000;

        if (flushing_cnt == 10000)
            fprintf (stderr, "Caffeine Sim - Flushing outstanding requests..\n");

        if (flushing_cnt-- == 0)
        {
            fprintf (stderr, " Done Flushing...\n");
            Manifold::Stop();
        }
    }
#endif
//////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
}

void Ticker::tock()
{
}
